package com.jinlia.show.modules.base.system.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinlia.show.modules.base.constant.EnableStatusEnum;
import com.jinlia.show.modules.base.model.po.GoodsAttrPO;
import com.jinlia.show.modules.base.system.entity.GoodsLabelRel;
import com.jinlia.show.modules.base.system.mapper.GoodsLabelRelMapper;
import com.jinlia.show.modules.base.model.po.GoodsLabelRelPO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class GoodsLabelRelMapperService extends ServiceImpl<GoodsLabelRelMapper, GoodsLabelRel> {
    final GoodsAttrMapperService goodsSkuMapperService;

    public Map<Long, List<GoodsLabelRelPO>> getGoodsGroupByTags(List<Long> tagIds) {
        /**
         *
         */
        List<GoodsLabelRelPO> list = this.baseMapper.getGoodsListByTags(tagIds, EnableStatusEnum.ENABLE.getValue());
        //填充 goodsPO de sku
        if (!CollectionUtils.isEmpty(list)) {
            List<GoodsAttrPO> pOsByGoodsId = goodsSkuMapperService.getPOsByGoodsId(list.stream().map(i -> i.getGoodsPO().getId()).distinct().toArray(Long[]::new));
            Map<Long, List<GoodsAttrPO>> collect = pOsByGoodsId.stream().collect(Collectors.groupingBy(GoodsAttrPO::getGoodsId));
            list.forEach(i -> {
                if (collect.containsKey(i.getGoodsPO().getId())) {
                    i.getGoodsPO().setGoodsAttrPOS(collect.get(i.getGoodsPO().getId()));
                }
            });
        }
        return list.stream().collect(Collectors.groupingBy(GoodsLabelRelPO::getLabelId));

    }
}
